package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.View;

import me.shurufa.R;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.fragments.NewLoginFragment;
import me.shurufa.fragments.NewRegisterFragment;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.Settings;
import me.shurufa.utils.StatusBarCompat;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 新登录界面
 */
public class NewLoginActivity extends BaseActivity implements View.OnClickListener {

    public int fromWhere;
    private int currentFragmentIndex = -1;
    private SparseArray<Fragment> fragments = new SparseArray<Fragment>(2);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_new_login;
        super.onCreate(savedInstanceState);
        // 提前加载一个避免卡顿
        switchFragment(0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (currentFragmentIndex == 0) {
            NewLoginFragment loginFragment = (NewLoginFragment) fragments.get(currentFragmentIndex);
            loginFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_login:
                switchFragment(0);
                break;
            case R.id.login_register:
                switchFragment(1);
                break;

            // 仅浏览 不登录
            case R.id.btn_enter_not_login:

                // 清除用户信息
                PersistenceUtils.logout();
                Intent intent = new Intent(NewLoginActivity.this, NewMainActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    }


    private void switchFragment(int id) {
        if (currentFragmentIndex == id) {
            return;
        }
        Fragment oldFragment = fragments.get(currentFragmentIndex);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (oldFragment != null)
            ft.hide(oldFragment);

        currentFragmentIndex = id;
        Fragment newFragment = fragments.get(currentFragmentIndex);
        if (newFragment == null) {
            switch (currentFragmentIndex) {
                case 0:
                    newFragment = new NewLoginFragment();
                    break;
                case 1:
                    newFragment = new NewRegisterFragment();
                    break;
            }
            fragments.put(currentFragmentIndex, newFragment);
        }

        if (newFragment != null) {
            if (!newFragment.isAdded()) {
                ft.add(R.id.fragment_container, newFragment, String.valueOf(currentFragmentIndex));
            }
            ft.show(newFragment);
        }
        ft.commitAllowingStateLoss();
    }

    public void onEventMainThread(Intent intent) {
        if (intent.getAction().equals(Constants.ACTION_LOGIN_SWITCH)) {
            switch (currentFragmentIndex) {
                case 0:
                    //registerButton.performClick();
                    switchFragment(1);
                    break;
                case 1:
                    //loginButton.performClick();
                    switchFragment(0);
                    break;
            }
        }

        if (Constants.ACTION_GO_LOGIN.equals(intent.getAction())) {
            this.fromWhere = Constants.TYPE_FROM_MAIN_TO_LOGIN;
        }

        if (Constants.ACTION_LOGIN_SUCCESS.equals(intent.getAction())) {
            startActivity(new Intent(this, NewMainActivity.class));
        }
    }

    @Override
    public void initStatusBar() {
        //StatusBarCompat.setColor(this, getResources().getColor(R.color.black_alpha_50));
        StatusBarCompat.setTransparent(this);
    }

}
